﻿using System;
using System.Net;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Documents;
using System.Windows.Ink;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Animation;
using System.Windows.Shapes;
using SilverTile.Sprites;
using SilverTile;
using System.Linq;

namespace SilverTileApp
{
    public class SwordSprite : AnimatedSprite
    {

        public const int SWORD_SPEED = 15;

        public Direction Direction { get; set; }
        public ISprite Player { get; set; }

        public Action<SwordSprite, ISprite> SwordHitMonster;
        public Action<SwordSprite> SwordHitWall;

        public Boolean Moving { get; set; }

        public SwordSprite(int width, int height)
            : base(width, height)
        {
            Direction = Direction.None;
            IsVisible = false;
        }

        public void SetDirection(Direction direction)
        {
            Direction = direction;
            SetActiveAnimation(direction.ToString());
        }

        public override void Update(TimeSpan timeSinceLastFrame)
        {

            if (IsVisible)
            {

                int dx = 0, dy = 0;

                if (Moving)
                {
                    switch (Direction)
                    {
                        case Direction.Up:
                            dy = -SWORD_SPEED;
                            break;
                        case Direction.Right:
                            dx = SWORD_SPEED;
                            break;
                        case Direction.Down:
                            dy = SWORD_SPEED;
                            break;
                        case Direction.Left:
                            dx = -SWORD_SPEED;
                            break;
                    }
                }

                var collisions = Viewport
                    .GetForegroundSpriteCollisions(this)
                    .Where(s => !(s is PlayerSprite) && !(s is SwordSprite))
                    .ToList();

                if (collisions.Count == 0)
                {
                    Collision collides = Viewport.SpriteWillCollideWithBackground(this, X + dx, Y + dy, 0);
                    if (X + dx < 0)
                    {
                        collides = collides.Set(Collision.Left);
                    }
                    else if (X + dx >= Viewport.Width)
                    {
                        collides = collides.Set(Collision.Right);
                    }

                    if (Y + dy < 0)
                    {
                        collides = collides.Set(Collision.Top);
                    }
                    else if (Y + dy >= Viewport.Height)
                    {
                        collides = collides.Set(Collision.Bottom);
                    }

                    if (collides.Collides() && SwordHitWall != null)
                    {
                        SwordHitWall(this);
                    }
                    else
                    {
                        X += dx;
                        Y += dy;
                    }
                }
                else
                {
                    if (collisions.Count > 0 && SwordHitMonster != null)
                    {
                        collisions.ForEach(s => SwordHitMonster(this, s));
                    }
                }

                base.Update(timeSinceLastFrame);

            }
        }

    }
}
